package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;
    @RequestMapping("/findBypage")
    public PageResult findBypage(@RequestBody QueryPageBean queryPageBean){
        //权限查询 查询所有权限以及与权限相关关联的角色
        PageResult pageResult= permissionService.findBypage(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/checkRepeat")
    public Result checkRepeat(String keyword,String id){

        if(id==null||"undefined".equals(id)){
            id=null;
        }

       return  permissionService.checkIsRepKey(keyword,id);

    }
    @RequestMapping("/deit")
    public Result deit(@RequestBody Permission permission){
        System.out.println(permission);

        try {
            permissionService.deit(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);

    }
    //编辑回显
    @RequestMapping("/echo")
    public Result echo(Integer id){

        Permission permission= null;
        try {
            permission = permissionService.findPermissionById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"数据回显失败",permission);
        }
//        System.out.println(roleName);
        return new Result(true,null,permission);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){


        try {
             permissionService.addPermission(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增权限失败",permission);
        }
//        System.out.println(roleName);
        return new Result(true,"新增权限成功",permission);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){


        return   permissionService.findPermissionContentById(id);


    }
}
